﻿using System;
using static NodeBasedSkillEditor.Model.Node.Node_ScalarToInteger;

namespace NodeBasedSkillEditor.Model.Node.RT
{
    public class Node_ScalarToInteger_RT : RT_Node
    {
        public RT_SingleValuePort In_Scalar;
        public RT_SingleValuePort In_Mode;
        public RT_SingleValuePort Out_Integer;

        public Node_ScalarToInteger_RT(Node_ScalarToInteger sourceData, RT_DAG parentDag) : base(sourceData, parentDag)
        {
            In_Scalar = new RT_SingleValuePort(EPortIOType.Input, sourceData.Scalar, this);
            In_Mode = new RT_SingleValuePort(EPortIOType.Input, sourceData.Mode, this);
            Out_Integer = new RT_SingleValuePort(EPortIOType.Output, sourceData.Integer, this);

            Out_Integer.OnGetValue = GetInteger;
        }

        public AValue GetInteger(DAG_Executor executor)
        {
            var scalarV = In_Scalar.GetValue(executor) as Value_Scalar;
            var integerV = Out_Integer.SourceValue as Value_Integer;

            var modeValue = In_Mode.GetValue(executor) as Value_Enum;
            EMode mode = (EMode)modeValue.Value;
            switch (mode)
            {
                case EMode.Floor:
                    integerV.Value = (int)MathF.Floor(scalarV.Value);
                    break;
                case EMode.Round:
                    integerV.Value = (int)MathF.Round(scalarV.Value);
                    break;
                case EMode.Ceiling:
                    integerV.Value = (int)MathF.Ceiling(scalarV.Value);
                    break;
                default:
                    break;
            }

            return Out_Integer.SourceValue;
        }
    }
}
